import { routerRedux } from 'dva/router';
import queryString from 'query-string';
import { getListData, addFlow, importFlow, addFlowNoCheck, delFlow, processDeploy, copyFlow, getModelVariables, processOffline, modelDetailList, checkModelExist} from "../server/FlowConfigureApi";

const initData = {
  flowListData: [],
  totalCount: 0,
  modelDetailList: [],
  modelTotalCount: 0,
};

export default {
  namespace: "FlowConfigure",
  state: {
    ...initData,
  },

  effects: {
    *getFlowListData({ payload }, { call, put }) {
      const { datas = {}, totalCount = 0 } = yield call(getListData, payload);
      yield put({ type: "get/flowListData", payload: datas || {}, totalCount });
    },
    *addOrCopyFlow({ payload, actionType }, { call, put }) {
      let callFn = addFlow;
      if (actionType === 'addNoCheck') { // 去掉cbd自带弹窗
        callFn = addFlowNoCheck;
      }
      if (actionType === 'copy') { // 复制流程
        callFn = copyFlow;
      }
      const res = yield call(callFn, payload);
      const { errCode, errMsg, errortext, datas } = res;
      if (errCode === 0) {
        const id = datas.modelId;
        yield put(routerRedux.push({
          pathname: '/flowmanage/flowchart',
          search: queryString.stringify({
            modelid: id,
          }),
          state: { isAdd: true },
        }));
      }
      return { errCode, errMsg: errMsg || errortext, datas }
    },
    *checkModelExist({ payload }, { call }) {
      const { errCode, errMsg, errortext, success, datas } = yield call(checkModelExist, payload);
      return { errCode, errMsg: errMsg || errortext, success, datas }
    },
    *importFlow({ payload }, { call, put }) {
      const res = yield call(importFlow, payload);
      const { errCode, errMsg, errortext, success, datas } = res && res.data;
      if (success === true) {
        const id = datas.modelId;
        yield put(routerRedux.push({
          pathname: '/flowmanage/flowchart',
          search: queryString.stringify({
            modelid: id,
          }),
          state: { isAdd: true },
        }));
      }
      return { errCode, errMsg: errMsg || errortext, success, datas }
    },
    *delFlow({ payload }, { call }) {
      const { errCode, errMsg } = yield call(delFlow, payload);
      return { errCode, errMsg }
    },
    *modelDetail({ payload }, { call }) {
      const { errCode, errMsg } = yield call(delFlow, payload);
      return { errCode, errMsg };
    },
    *processDeploy({ payload }, { call }) {
      const { errCode, errMsg, errortext } = yield call(processDeploy, payload);
      return { errCode, errMsg: errMsg || errortext }
    },
    *getModelVariables({ payload }, { call }) {
      const { errCode, errMsg, errortext, datas } = yield call(getModelVariables, payload);
      return { errCode, errMsg: errMsg || errortext, datas }
    },
    *processOffline({ payload }, { call }) {
      const { success, errMsg, errortext } = yield call(processOffline, payload);
      return { success, errMsg: errMsg || errortext }
    },
    *modelDetailList({ payload }, { call, put }) {
      const { datas = {}, totalCount = 0 } = yield call(modelDetailList, payload);
      yield put({ type: "get/modelDetailList", payload: datas || [], totalCount });
    },
  },
  reducers: {
    "get/flowListData": (state, { payload, totalCount }) => ({
      ...state,
      flowListData: payload,
      totalCount,
    }),
    "get/modelDetailList": (state, { payload, totalCount }) => ({
      ...state,
      modelDetailList: payload,
      modelTotalCount: totalCount,
    }),
    clearData: () => ({ ...initData }),
  },
};
